<template>
	<u-popup v-model="value" mode="bottom" :popup="false" :mask="true" :closeable="true" :safe-area-inset-bottom="true"
		close-icon-color="#ffffff" :z-index="uZIndex" :maskCloseAble="maskCloseAble" @close="close">
		<view class="box">
			<u-tabs v-if="value" active-color="#F09238" :list="genTabsList" :is-scroll="true" :current="tabsIndex"
				@change="tabsChange" ref="tabs"></u-tabs>
		</view>
		<view class="area-box">
			<view class="u-flex" :class="{ 'change':isChange }">
				<view class="area-item">
					<view class="u-padding-10 u-bg-gray" style="height: 100%;">
						<scroll-view :scroll-y="true" style="height: 100%">
							<u-cell-group>
								<u-cell-item v-for="(item,index) in provinces" :title="item.name" :arrow="false"
									:index="index" :key="index" @click="provinceChange">
									<u-icon v-if="isChooseP&&province===index" slot="right-icon" size="34"
										name="checkbox-mark"></u-icon>
								</u-cell-item>
							</u-cell-group>
						</scroll-view>
					</view>
				</view>
				<view class="area-item">
					<view class="u-padding-10 u-bg-gray" style="height: 100%;">
						<scroll-view :scroll-y="true" style="height: 100%">
							<u-cell-group v-if="isChooseP">
								<u-cell-item v-for="(item,index) in citys" :title="item.name" :arrow="false"
									:index="index" :key="index" @click="cityChange">
									<u-icon v-if="isChooseC&&city===index" slot="right-icon" size="34"
										name="checkbox-mark"></u-icon>
								</u-cell-item>
							</u-cell-group>
						</scroll-view>
					</view>
				</view>

				<!-- <view class="area-item">
					<view class="u-padding-10 u-bg-gray" style="height: 100%;">
						<scroll-view :scroll-y="true" style="height: 100%">
							<u-cell-group v-if="isChooseC">
								<u-cell-item v-for="(item,index) in areas" :title="item.name" :arrow="false"
									:index="index" :key="index" @click="areaChange(item,index)">
									<u-icon v-if="isChooseA&&area===index" slot="right-icon" size="34"
										name="checkbox-mark"></u-icon>
								</u-cell-item>
							</u-cell-group>
						</scroll-view>
					</view>
				</view> -->
				<!-- <view class="area-item">
					<view class="u-padding-10 u-bg-gray" style="height: 100%;">
						<scroll-view :scroll-y="true" style="height: 100%">
							<u-cell-group v-if="isChooseC">
								<u-cell-item v-for="(item,index) in streets" :title="item.name" :arrow="false"
									:index="index" :key="index" @click="streetChange(item,index)">
									<u-icon v-if="isChooseS&&street===index" slot="right-icon" size="34"
										name="checkbox-mark"></u-icon>
								</u-cell-item>
							</u-cell-group>
						</scroll-view>
					</view>
				</view> -->
			</view>
		</view>
	</u-popup>
</template>

<script>
	import {
		province,
		city,
		area,
		street
	} from "../api/index.js"
	export default {
		name: 'u-city-select',
		props: {
			// 通过双向绑定控制组件的弹出与收起
			value: {
				type: Boolean,
				default: false
			},
			// 默认显示的地区，可传类似["河北省", "秦皇岛市", "北戴河区"]
			defaultRegion: {
				type: Array,
				default () {
					return [];
				}
			},
			// 默认显示地区的编码，defaultRegion和areaCode同时存在，areaCode优先，可传类似["13", "1303", "130304"]
			areaCode: {
				type: Array,
				default () {
					return [];
				}
			},
			// 是否允许通过点击遮罩关闭Picker
			maskCloseAble: {
				type: Boolean,
				default: true
			},
			// 弹出的z-index值
			zIndex: {
				type: [String, Number],
				default: 0
			}
		},
		data() {
			return {
				cityValue: "",
				isChooseP: false, //是否已经选择了省
				province: 0, //省级下标
				provinces: [],
				isChooseC: false, //是否已经选择了市
				city: 0, //市级下标
				citys:[],
				isChooseA: false, //是否已经选择了区
				area: 0, //区级下标
				areas: [],
				street: 0,
				streets: [],
				isChooseS: false,
				tabsIndex: 0
			}
		},
		mounted() {
			this.getprovince()
			this.init();
		},
		computed: {

			isChange() {
				return this.tabsIndex > 1;
			},
			genTabsList() {
				let tabsList = [{
					name: "请选择"
				}];
				if (this.isChooseP) {
					tabsList[0]['name'] = this.provinces[this.province]['name'];
					tabsList[1] = {
						name: "请选择"
					};
				}
				// if (this.isChooseC) {
				// 	tabsList[1]['name'] = this.citys[this.city]['name'];
				// 	tabsList[2] = {
				// 		name: "请选择"
				// 	};
				// }
				// if (this.isChooseA) {
				// 	tabsList[2]['name'] = this.areas[this.area]['name'];
				// 	tabsList[3] = {
				// 		name: "请选择"
				// 	};
				// }
				// if (this.isChooseS) {
				// 	tabsList[3]['name'] = this.streets[this.street]['name'];
				// }
				return tabsList;
			},
			uZIndex() {
				// 如果用户有传递z-index值，优先使用
				return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
			}
		},
		methods: {
			// huoqu省
			async getprovince() {
				let res = await this.get(province)
				this.provinces = res.data
			},
			async getcity(id) {
				let res = await this.get(city, {
					provinceId: id
				})
				this.citys =[... res.data]
			},
			async getarea(id) {
				let res = await this.get(area, {
					cityId: id
				})
				this.areas = [{name:"全部",regionId:null},... res.data]
			},
			async getstreets(id) {
				let res = await this.get(street, {
					areaId: id
				})
				this.streets = [{name:"全部",regionId:null},... res.data]
			},
			init() {
				if (this.areaCode.length == 3) {
					this.setProvince("", this.areaCode[0]);
					this.setCity("", this.areaCode[1]);
					this.setArea("", this.areaCode[2]);
				} else if (this.defaultRegion.length == 3) {
					this.setProvince(this.defaultRegion[0], "");
					this.setCity(this.defaultRegion[1], "");
					this.setArea(this.defaultRegion[2], "");
				};
			},
			setProvince(label = "", value = "") {
				this.provinces.map((v, k) => {
					if (value ? v.value == value : v.label == label) {
						this.provinceChange(k);
					}
				})
			},
			setCity(label = "", value = "") {
				this.citys.map((v, k) => {
					if (value ? v.value == value : v.label == label) {
						this.cityChange(k);
					}
				})
			},
			setArea(label = "", value = "") {
				this.areas.map((v, k) => {
					if (value ? v.value == value : v.label == label) {
						this.isChooseA = true;
						this.area = k;
					}
				})
			},
			close() {
				this.$emit('isshow', false);
			},
			tabsChange(index) {
				this.tabsIndex = index;
			},
			provinceChange(index) {
				this.isChooseP = true;
				this.isChooseC = false;
				this.isChooseA = false;
				this.isChooseS = false;
				this.getcity(this.provinces[index].code)
				this.province = index;
				this.tabsIndex = 1;
			},
			cityChange(index) {
				console.log(this.citys[index])
				if(this.citys[index].regionId==null){
					this.isChooseC = true;
					let result = {};
					result.regionId= this.provinces[this.province].regionId
					result.name= this.provinces[this.province].name
					result.type= 1
					this.$emit('city-change', result);
					this.close();
				}else{
					this.isChooseC = true;
					this.isChooseA = false;
					this.isChooseS = false;
					this.city = index;
					let result = {};
					result.regionId= this.citys[this.city].regionId
					result.name= this.citys[this.city].name
					// result.type= 1
					this.$emit('city-change', result);
					this.close();
					// this.getarea(this.citys[index].code)
					// this.tabsIndex = 2;
				}
				
			},
			// areaChange(item, index) {
			// 	console.log(item)
			// 	if(item.regionId==null){
			// 		this.isChooseA = true;
			// 		let result = {};
			// 		result.regionId= this.citys[this.city].regionId
			// 		result.name= this.citys[this.city].name
			// 		result.type= 2
			// 		this.$emit('city-change', result);
			// 		this.close();
			// 	}else{
			// 		this.isChooseA = true;
			// 		this.isChooseS = false;
			// 		this.area = index;
			// 		this.getstreets(item.code)
			// 		this.tabsIndex = 3
			// 	}
			// },
			// streetChange(item, index) {
			// 	if(item.regionId==null){
			// 		this.isChooseS = true;
			// 		let result = {};
			// 		result.regionId= this.areas[this.area].regionId
			// 		result.name= this.areas[this.area].name
			// 		result.type= 3
			// 		this.$emit('city-change', result);
			// 		this.close();
			// 	}else{
			// 		this.street = index
			// 		this.isChooseS = true;
			// 		let result = {};
			// 		result.regionId= this.streets[this.street].regionId
			// 		result.name= this.streets[this.street].name
			// 		result.type= 4
			// 		this.$emit('city-change', result);
			// 		this.close();
			// 	}
			// }
		}

	}
</script>
<style lang="scss">
	.box{
		display: flex;
		justify-content: center;
	}
	.area-box {
		width: 100%;
		overflow: hidden;
		height: 800rpx;

		>view {
			width:100%;
			transition: transform 0.3s ease-in-out 0s;
			transform: translateX(0);

			&.change {
				transform: translateX(-50%);
			}
		}

		.area-item {
			width: 50%;
			height: 800rpx;
		}
	}
</style>
